package com.eastmoney.myapplication

import android.content.Intent
import android.net.VpnService
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import  androidx.compose.material3.Button
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color

import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.eastmoney.myapplication.ui.theme.MyApplicationTheme
import com.eastmoney.vpn.emguard
import vpn.Vpn

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        println("你好世界")
        setContent {
            MyApplicationTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { padding->
                    Box(Modifier.padding(padding)) {
                        Page(::start)
                    }

                }
            }
        }
    }
    fun start(){
        println("World!--------")
        println(Vpn.log())
        startVpnService()
    }

    // 定义ActivityResultLauncher
    private val vpnPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        when (result.resultCode) {
            RESULT_OK -> {
                // 直接启动服务
                Intent(this, GoVpnService::class.java).also {
                    startService(it)
                }
            }
            else -> {
                Toast.makeText(this, "VPN permission denied", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun startVpnService() {
        VpnService.prepare(this)?.let { intent ->
            println("弹窗确认")
            vpnPermissionLauncher.launch(intent)
        } ?: run {
            println("直接开启")
            // 如果已经有权限，直接启动服务
            Intent(this, GoVpnService::class.java).also {
                println("启动服务的Intent: $it")
                startService(it)
            }
        }
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        Page({})
    }
}

@Composable
fun Page(onClick: () -> Unit ) {
    Column(Modifier.padding(20.dp)) {
        Row ( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
            Button(onClick =onClick) {
                Text("开启vpn")
            }
        }
    }
}